package com.example.springbootdemo.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

//@Component
@Slf4j
public class BeanStartTime implements BeanPostProcessor {
    private long startTime;
    private long endTime;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        startTime = System.currentTimeMillis();
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        endTime = System.currentTimeMillis();
        log.info(beanName + "初始化耗时：" + (endTime - startTime) + " ms");
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
}
